home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / asm / adisv1_3.lha / ADis.doc next >
Encoding:
Text File  |  1995-01-14  |  16.4 KB  |  372 lines

  1.  
  2.  
  3.  
  4.                    ADis (Advanced Disassembler) User's Guide
  5.                                 Version 1.3
  6.                          $Date: 93/09/24 17:54:43 $
  7.                            written by Martin Apel
  8.                         email: apel@physik.uni-kl.de
  9.  
  10.  
  11. CONTENTS
  12.   1. Introduction
  13.   2. Command line options
  14.   3. Hints and Problems
  15.   4. Reassembly
  16.   5. Theory of operation
  17.   6. Known bugs
  18.   7. Future plans
  19.   8. Acknowledgments
  20.   9. Miscellaneous
  21.  
  22.  
  23. *************************************************************************
  24.  
  25.  IMPORTANT NOTICE:  This program is copyrighted by Martin Apel, but can
  26.  be freely distributed, providing that the following rules are respected.
  27.  - Every form of distribution is allowed and encouraged, but no fee can 
  28.    be charged for this program except for, possibly, the cost of magnetic 
  29.    media and/or disk duplication and shipping.
  30.  - Inclusion in PD software libraries such as Fish Disks is allowed, 
  31.    provided the fees charged for these disks are comparable with those
  32.    charged by Fred Fish.
  33.  - The program cannot be distributed in any commercial product without the 
  34.    written consent of the author.
  35.  - If you modify the source or include it in a program of yours you should
  36.    send me a copy of your program. You also have to indicate in your
  37.    documentation that your work is based on ADis written by Martin Apel.
  38.  
  39.  By copying, distributing and/or using the program you indicate your 
  40.  acceptance of the above rules.
  41.  
  42.  
  43. *************************************************************************
  44.  
  45. 1. INTRODUCTION
  46.  
  47. ADis  is  a 68000+ disassembler which can automatically recognize data
  48. and  strings  put into the code segment.  It also generates only those
  49. labels  that  are really referenced.  The generated file will often be
  50. reassemblable.   In  V1.2  ADis  is  capable  of recognizing all 68020
  51. /030/040  and  68881  instructions  even  with  the  68020's  extended
  52. addressing  modes.   ADis will also try to resolve addressing relative
  53. to a4, which many C compilers use in a small memory model.
  54.  
  55. If  you  have  ever  worked  with  a  usual disassembler, you may have
  56. wondered  about all those nasty ORI.B #0,D0 or BVS $4711 instructions.
  57. The  first example is valid, but not very useful.  ADis will recognize
  58. this  and  generate  a  DC.W  0  instead.   The second example is even
  59. illegal, as it branches to an odd address.
  60.  
  61.  
  62. *************************************************************************
  63.  
  64. 2. COMMAND LINE OPTIONS
  65.  
  66. I  will list the possible options for a quick look here.  They will be
  67. explained in detail later.  The following options are available:
  68.  
  69.      -a                               add absolute offset in file as comment
  70.      -b num                           set buffersize for file buffers in KB
  71.                                       default: 10 KB
  72.      -c2                              enable 68020 instruction disassembly
  73.      -c3                              enable 68030 instruction disassembly
  74.      -c4                              enable 68040 instruction disassembly
  75.      -c8                              enable 68881 instruction disassembly
  76.      -dl                              disassemble as library (or device)
  77.      -fs                              put hunks in a single file
  78.      -fm                              put each hunk in a separate file
  79.      -i                               print addresses where illegal
  80.                                       instructions were found
  81.      -lc hex_address                  disassemble as code
  82.      -ld hex_address                  disassemble as data
  83.      -ln                              name labels with ascending numbers
  84.                                       instead of code addresses
  85.      -ml                              disassemble using large memory model
  86.      -ms[base_offset]                 attempt to address code and data 
  87.                                       relative to A4
  88.      -o outfilename                   filename of output file
  89.                                       default: <filename>.dec
  90.      -q                               quick disassembly, no labels
  91.                                       no data recognition in code segments
  92.      -v                               verbose
  93.  
  94.  
  95. Option -a:
  96.   The  absolute  offset  relative  to the start of the load file will be
  97.   written  as  a comment after each instruction.  This is useful, if you
  98.   want to edit the load file in a file monitor and modify it.
  99.  
  100. Option -b n:
  101.   You  can  set  the  size of the output buffer to increase the speed of
  102.   writing.  Default is 10 KB.
  103.  
  104. Option -c2:
  105.   This  option  will  enable 68020 opcodes and address register indirect
  106.   with  scaled  index addressing modes.  It does not enable the extended
  107.   instruction  modes (see -ce).  If not set, all 68020 instructions will
  108.   be recognized as illegal, and printed as data.
  109.  
  110. Option -c3:
  111.   This  one  additionally  enables 68030 instructions (MMU opcodes).  It
  112.   also enables the extended 68020 addressing modes.
  113.  
  114. Option -c4:
  115.   Enables 68040 disassembly. Automatically sets -c8 switch (see below).
  116.   It also enables the extended 68020 addressing modes.
  117.  
  118. Option -c8:
  119.   This  option  enables 68881 opcodes.  If not set, all FPU opcodes will
  120.   be disassembled as data.
  121.  
  122. Option -dl:
  123.   ADis  now  supports  disassembly  of libraries and devices.  With this
  124.   option  ADis  recognizes the ROMTag at the start of the file and reads
  125.   the jump table.  It places a named label at the start of each function
  126.   called through the function table.
  127.  
  128. Option -fs:
  129.   ADis  can either put all hunks into a single output file or generate a
  130.   separate  file  for  each hunk.  The second option comes in handy when
  131.   disassembled files grow so large that they aren't easily processed any
  132.   further  if  everything is put together.  The default is to generate a
  133.   single  file  if  not  more  than  one hunk of each type exists in the
  134.   program  (CODE, DATA, BSS).  This default can be overridden by the -fs
  135.   or  -fm  option.   -fs  forces  ADis to generate a single file even if
  136.   there  are  multiple  hunks  of  the same type.  -fm forces to write a
  137.   separate file for each hunk even if there are only few of them.
  138.  
  139. Option -fm:
  140.   See option -fs.
  141.  
  142. Option -i:
  143.   This option will cause ADis to print its current address every time it
  144.   steps  onto  an illegal instruction.  This will make it easier for you
  145.   to set additional labels to aid ADis.
  146.  
  147. Option -lc hex_address:
  148.   ADis  keeps  a  table  of  all references.  If it doesn't recognize an
  149.   instruction  as code, it will print it as data.  To help ADis with its
  150.   task  to  recognize  code,  you  can  set  a reference at that address
  151.   telling it that this address is meant as code.
  152.  
  153. Option -ld hex_address:
  154.   Generates  a  label  in  ADis' internal tables, which will force it to
  155.   write  a  label out for that address and interpret the bit combination
  156.   at  that address as being data.  However you cannot force something to
  157.   be  disassembled  as data, if it is valid code.  It's simply an aid to
  158.   ADis where to start a new try for disassembly.
  159.  
  160. Option -ln:
  161.   Instead  of  labelling  each  instruction  with  its code address this
  162.   option  instructs  ADis  to  use ascending labels.  Useful for 'diffs'
  163.   between the original and a modified version of the same program.
  164.  
  165. Option -ml:
  166.   By  default, ADis assumes that the program to be disassembled has been
  167.   compiled  using  a  small  memory  model with A4 as its base register.
  168.   Many  times  this  default  is  very useful, but if either it has been
  169.   compiled  with  the  large  memory  model  or  it  has been hand-coded
  170.   assembler or the like, the -ml option will forbid this default.
  171.  
  172. Option -ms:
  173.   By  default, ADis assumes that the program to be disassembled has been
  174.   compiled  using a small memory model with A4 as its base register.  It
  175.   tries  to  guess  the contents of A4 to generate correct labels.  When
  176.   you  see  many  parts  of the program being disassembled as data, this
  177.   value  is  probably  wrong.   Then you have to look for a "LEA xxx,A4"
  178.   instruction  or  the like and give the value "xxx" as parameter to the
  179.   -ms  option.  If ADis never sees an instruction of the form "LEA ,A4",
  180.   it will generate a warning that it might be more useful to use the -ml
  181.   switch.
  182.  
  183. Option -o filename:
  184.   This  option is easy to explain:  It sets the name of the output file.
  185.   Default is <input file>.dec.
  186.  
  187. Option -q:
  188.   Quick  disassembly.   When  this  option  is set, the analysis will be
  189.   skipped and everything inside the code segment will be disassembled as
  190.   code  if  possible.   Illegal  instructions  will  be printed as "DC.W
  191.   hex_val".
  192.  
  193. Option -v:
  194.   Lets ADis display verbose information.  Working on long files ADis may
  195.   produce  no  output for longer time and not access disks, so you might
  196.   think  it  isn't  doing  anything.   With  -v  it displays the current
  197.   address it is working on.
  198.  
  199. Additionally to those command line options the analysis pass can be
  200. terminated by pressing CTRL-D. This will cause the not yet analyzed
  201. part of the program to be disassembled as data.
  202.  
  203. *************************************************************************
  204.  
  205. 3. HINTS AND PROBLEMS
  206.  
  207. Because  ADis  doesn't really understand the code it is disassembling,
  208. it  is  possible that it makes a few mistakes, which in general aren't
  209. very  important,  but  they can be very annoying.  To circumvent this,
  210. there  are  a  few  options  which will modify ADis' behaviour in such
  211. cases.   There  are four problematic areas, in which ADis may generate
  212. strange disassemblies:
  213.   - Code generated by switch statements (PC relative)
  214.   - Code referenced only through jump tables (Relocated references)
  215.   - Different optimizations used by the original assembler the code was
  216.     produced with and the assembler you are using for reassembly.
  217.   - Unterminated code threads
  218.  
  219.  
  220. 3.1 Code generated by C switch statements (PC relative)
  221.  
  222. ADis  can  recognize jump tables in most cases.  However, as there are
  223. several  common  types  of  jump table constructions, it might fail (I
  224. have  only  observed  that with quite old programs that must have been
  225. compiled with a compiler that is not use anymore).  This might lead to
  226. ordinary  statements  disassembled  as  data,  because  they are never
  227. referenced  directly  by  any code.  You can change that by looking at
  228. the  output  and  place a code label at the corresponding address with
  229. the -lc option.
  230.  
  231.  
  232. 3.2 Code referenced only through jump tables (Relocated references)
  233.  
  234. When  the disassembled program uses function pointers and jump tables,
  235. ADis  will only generate data references for such locations.  To force
  236. these  to  be  disassembled  as  code  you can place a code label at a
  237. certain address with the -lc option.
  238.  
  239.  
  240. 3.3 Different optimizations used by the original assembler the code was
  241.     produced with and the assembler you are using for reassembly
  242.  
  243. Most  assemblers  do  not  allow  instructions  to  be  specified very
  244. detailed.   A  "MOVE  $4,A6" instruction (a very common instruction on
  245. the Amiga) might be assembled as an absolute word or long reference to
  246. address  4.  This will change the length of the code generated and all
  247. subsequent  labels to be moved.  If this happens often, it might occur
  248. that  a branch might specifiy a destination outside of its range, i.e.
  249. a branch to an instruction just below 32K away in the original program
  250. might  lead  to  an  instruction more than 32K away in the reassembled
  251. one.   Compilers  do  not  have  this  problem,  because  most of them
  252. generate JMP or JSR instructions which will be optimized to BRA or BSR
  253. by the assembler if possible.
  254. If  you  ever  have  problems  with  this  kind of output, you have to
  255. substitute the branches with equivalent jumps constructions.
  256.  
  257.  
  258. 3.4 Unterminated code threads
  259.  
  260. ADis  assumes  all code threads to be terminated by a RTS, JMP, BRA...
  261. instruction,  i.e.   an  instruction  that unconditionally changes the
  262. control  flow  and  does  not  return.   Some  compilers such as GNU C
  263. generate  special code for the exit statement:  They generate a JSR to
  264. the  exit function, but since this will never return, there is no code
  265. following  the  JSR.   This  will cause ADis to run into the following
  266. locations,  which  might  be data.  Such data often represents illegal
  267. code  and  therefore  the whole thread will be marked as illegal.  The
  268. same occurs for code sequences such as
  269.           BEQ       label1
  270.           BNE       label2
  271. Neither  of  these instructions is an "end instruction" and the thread
  272. is assumed to be continued after that.  In a future version the second
  273. possibility  might  be  recognized as terminating a thread.  Until now
  274. there  is no option to fix the first case; in a future version I might
  275. add an option for generating artificial "end instructions".
  276.  
  277.  
  278. *************************************************************************
  279.  
  280. 4. REASSEMBLY
  281.  
  282. Reassembly  is  somewhat  complicated,  because  I  haven't  found  an
  283. assembler,  which fulfills all requirements.  Some of the available PD
  284. assemblers  such  as  "DAS"  from DICE and "NCode" load the whole file
  285. into  memory,  which  is  impossible for the large files, which may be
  286. generated  by ADis.  Others (e.g.  A68k) will not accept more than one
  287. segment  of  each type in one file.  Still others (e.g.  ASM68k) don't
  288. support  68020  and  68881  opcodes.   When  I  have found a usable PD
  289. assembler  I  will  adapt  ADis  to  directly  generate  code for that
  290. assembler.   For  the  time  being,  the  disassembled  code has to be
  291. modified  to  be understood by different assemblers.  However the only
  292. sections  that  need  to be changed should the NEAR and FAR directives
  293. and  maybe  cross-references.   I plan to add a configuration file for
  294. ADis  where  the  strings  that  are  written  for  the  NEAR  and FAR
  295. directives can be specified.
  296.  
  297.  
  298. *************************************************************************
  299.  
  300. 5. THEORY OF OPERATION
  301.  
  302. ADis makes three passes through the program.  The first one only reads
  303. the  relocation  and symbol hunks and enters them into a symbol table.
  304. The second pass analyzes the code and stores the type of each location
  305. (code  or  data)  in  a temporary file.  The third pass writes out the
  306. code and data using the temporary file generated in the second pass.
  307.  
  308.  
  309. Analysis
  310.  
  311. The analysis is sort of a backtracking algorithm.  It assumes that the
  312. first  location in the first code segment is executable and traces all
  313. jumps  and  branches from there on recursively.  When stepping onto an
  314. illegal  instruction  it  takes back all decisions that lead there and
  315. flags  the  offending  location as data.  There are several heuristics
  316. which  are used when every code thread has been followed and there are
  317. still  undetermined  parts of the file.  E.g.  it will assume the next
  318. instruction  after  a  RTS  or  JMP  as code, tries to follow it until
  319. either  having  followed all threads again or stepping onto an illegal
  320. instruction.   During  all  this  trial  and error it takes the labels
  321. generated  so  far  as  an orientation where to try disassembling.  By
  322. inserting  labels  "by  hand"  you  can  modify  the  behaviour of the
  323. analysis  pass.   You can also mislead ADis, for instance by placing a
  324. code label at the second word of a legal instruction.  This will cause
  325. ADis  to  take  back  all its decisions, because an instruction is not
  326. allowed to cross a label.
  327.  
  328.  
  329. *************************************************************************
  330.  
  331. 6. KNOWN BUGS
  332.  
  333. Currently there are no known bugs.
  334.  
  335.  
  336. *************************************************************************
  337.  
  338. 7. FUTURE PLANS
  339.  
  340. There  are  several  things  I still want to add to ADis including the
  341. following:
  342.  
  343. - An option to generate artificial "end instructions" (s. 3.4)
  344. - Optionally  generate  tabs instead of spaces to reduce the size of the
  345.   disassembled file.
  346. - Add  a  configuration  file.   The command line tends to become rather
  347.   long, so standard options will be read from that file.
  348.  
  349.  
  350. *************************************************************************
  351.  
  352. 8. ACKNOWLEDGMENTS
  353.  
  354. Many  thanks  to Daniel Weber for supplying me with the opcodes of the
  355. 68030/040  processors  and doing the beta testing.  Thanks also to all
  356. those who sent me bug reports and suggestions for new features.
  357.  
  358.  
  359. *************************************************************************
  360.  
  361. 9. MISCELLANEOUS
  362.  
  363. Any hints, bugs, praise,... may be addressed to 
  364.                     apel@physik.uni-kl.de
  365. When  reporting bugs, please include the program that was disassembled
  366. incorrectly  or  otherwise revealed a bug in ADis (if possible).  Also
  367. include  the version number of ADis that produced the error.  Requests
  368. for future enhancements are also welcome.
  369.  
  370. -----------
  371. Martin Apel
  372.